package personal.wang.dic;

import org.apache.commons.io.FileUtils;
import personal.wang.utils.JDBCUtil;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * 初始化词典数据 /dictionarys
 * @author： 王义飞
 */
public class DicInitializer implements Serializable {
	public static void loadDics(Map<String,String> map) {
		Properties properties = new Properties();
		properties.putAll(map);
		Dictionary.DictionarySingleton.getInstance().addDic(properties);
	}

	public static void loadDics(String dicDir) {
		File dics = new File(dicDir);
		if (dics.exists() && dics.isDirectory()) {
			String[] files = dics.list();
			try{
				for (String dic : files) {
					// System.out.println("dic:" + dic);
					if(dic.endsWith(".dic")){
						Dictionary.DictionarySingleton.getInstance().addDic(
								loadDic(dicDir + File.separator + dic)
						);
					}
				}
			}catch (Exception e){
				e.printStackTrace();
			}
		}
	}

	/**
	 * todo: 在这里加载子字典文件的内容，在 execute 函数里面将内容替换掉。
	 * */
	public static void loadSubDic(String key , Properties p , String path){
		String value = (String)p.get(key);
		String subDicPath = null ;
		List<String> rs = new ArrayList<>();
		for (String s : value.split("\\|\\|\\|")) {
			subDicPath = path + File.separator +s.replaceAll("\\$File\\{", "")
					.replaceAll("\\}","") + ".dic";
			try{
				String cnt = FileUtils.readFileToString(new File(subDicPath));
				rs.add(cnt.replaceAll("\\s",""));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		p.setProperty(key , rs.stream().collect(Collectors.joining("|||")));
	}

	public static Properties loadDic(String path) {
		Properties p = new Properties();
		String subDicPath = null ;
		try(InputStream is = new FileInputStream(path);){
			p.load(is);
			for (Map.Entry<Object, Object> entry: p.entrySet()) {
				 if(entry.getValue().toString().contains("$DB{")){
					String s = loadValuesFromDB(entry.getValue().toString());
					p.setProperty(entry.getKey().toString() , s);
				 }else if(entry.getValue().toString().contains("$File{")){
					 subDicPath = new File(path).getParent();
					 loadSubDic( (String)entry.getKey() , p , subDicPath);
				 }
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return p;
	}

	private static String loadValuesFromDB(String sql) {
		String executeSql = sql.replace("$DB{" , "")
				.replace("}" , "");
		ResultSet rs = null ;
		List<String> values = new ArrayList();
		try(
                Connection connection = JDBCUtil.getConnection();
                Statement stmt = connection.createStatement();
		){
			rs = stmt.executeQuery(executeSql);
			while(rs.next()){
				values.add(rs.getString("value"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return values.stream().collect(Collectors.joining("|||"));
	}
}
